package locations.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import locations.model.TipoProblematica;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import locations.dao.implement.ProblematicTypeDAOHibernateJPA;

public class SaveProblematicTypeAction extends ActionSupport 
        implements ModelDriven<TipoProblematica>, ServletRequestAware, ServletResponseAware
{

    private static final long serialVersionUID = -8124967730231198191L;
	
    private HttpServletRequest request; 
    private HttpServletResponse response; 
    private TipoProblematica problematicType = new TipoProblematica();

    public String execute()
    {
        ProblematicTypeDAOHibernateJPA dao = new ProblematicTypeDAOHibernateJPA();
        dao.save(this.problematicType);
        return SUCCESS;
    }

    public TipoProblematica getProblematicType()
    {
        return problematicType;
    }

    public void setProblematicType(TipoProblematica problematicType)
    {
        this.problematicType = problematicType;
    }

    @Override
    public TipoProblematica getModel()
    {
        return this.problematicType;
    }

    public HttpServletRequest getRequest()
    {
        return request;
    }

    public void setRequest(HttpServletRequest request)
    {
        this.request = request;
    }

    public void setResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public HttpServletResponse getResponse()
    {
        return this.response;
    }

    @Override
    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    @Override
    public void setServletRequest(HttpServletRequest request)
    {
        this.request = request;
    }
    
    public void validate()
    {
        boolean ok = true;
        String name = this.getProblematicType().getNombre();
        String description = this.getProblematicType().getDescripcion();
        if (name.equals(""))
        {
            addFieldError("nombre", "El nombre no puede estar en blanco");
                ok = false;
        }
        if (description.equals(""))
        {
            addFieldError("descripcion", "La descripcion no puede estar en blanco");
                ok = false;
        }
        if (ok)
        {
            ProblematicTypeDAOHibernateJPA dao = new ProblematicTypeDAOHibernateJPA();
            long auxId = (this.getProblematicType().getId() == null? 0 : this.getProblematicType().getId());
            if (dao.exists(name, auxId))
            {
                addFieldError("nombre", "Ya existe un tipo de problematica con ese nombre");
                ok = false;
            }
        }
    }
    
}
